//package com.boot2.core.conf;
//
//import com.boot2.core.dao.redis.ProtoStuffRedisSerializer;
//import com.boot2.core.dao.redis.StringRedisSerializer;
//import com.boot2.core.dao.redis.core.manager.HlCacheManager;
//import com.boot2.core.dao.redis.core.manager.LayeringCacheManager;
//import com.boot2.core.dao.redis.tools.CacheMonitorProperties;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.cache.CacheManager;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.cache.interceptor.KeyGenerator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//
//import java.lang.reflect.Method;
//
///**
// * @author zhangweilin
// * @ClassName: RedisConfig
// * @description: redis
// * @date 2017年12月7日 下午2:58:39
// */
//@Configuration
//@ConditionalOnProperty("spring.redis.host")
////@EnableConfigurationProperties({LayeringCacheProperties.class})
////@Import({LayeringCacheServletConfiguration.class})
////@Conditional(CacheCondition.class)
//@EnableCaching
//public class RedisConfig extends CachingConfigurerSupport {
//
//    @Override
//    @Bean
//    public KeyGenerator keyGenerator() {
//        return new KeyGenerator() {
//            @Override
//            public Object generate(Object target, Method method, Object... params) {
//                StringBuilder sb = new StringBuilder();
//                sb.append(target.getClass().getName());
//                sb.append(method.getName());
//                for (Object obj : params) {
//                    sb.append(obj.toString());
//                }
//                return sb.toString();
//            }
//        };
//    }
//
//    ////====spring boot 2以上 版本可用
////    @Bean
////    public HlCacheManager cacheMRedisCacheConfigurationanager(RedisConnectionFactory redisConnectionFactory) {
//////        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
////        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(":");
////        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)).cacheDefaults(redisCacheConfiguration).build();
////    }
////    //====spring boot 1.5.15版本可用
//    @SuppressWarnings("rawtypes")
//    @Bean
//    public CacheManager cacheManager(RedisTemplate redisTemplate) {
//        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
//        //指定redis事务与spring的事务联动，即数据成功提交才入缓存，如果事务有回滚，缓存也同步回滚
//        redisCacheManager.setTransactionAware(true);
//        //指定缓存的前缀，默认值为DefaultRedisCachePrefix,如果在业务开发中指定了缓存的名称，
//        // 且名称里有冒号，则会用冒号自动分组(默认的分隔符为冒号，可以修改)，建议将缓存名称配置在类上而不是方法上，这样一个类一个缓存名称，也即一个组
//        redisCacheManager.setUsePrefix(true);
////        redisCacheManager.setCachePrefix();
//        // 设置缓存过期时间
//        // rcm.setDefaultExpiration(60);//秒
//        return redisCacheManager;
//    }
//
////    @Bean
////    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
////        StringRedisTemplate template = new StringRedisTemplate(factory);
////        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
////        ObjectMapper om = new ObjectMapper();
////        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
////        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
////        jackson2JsonRedisSerializer.setObjectMapper(om);
////        template.setValueSerializer(jackson2JsonRedisSerializer);
////        template.afterPropertiesSet();
////        return template;
////    }
//
//    @Bean
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//
//        // 设置键（key）的序列化采用StringRedisSerializer。
//        StringRedisSerializer serializer = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(serializer);
//        redisTemplate.setHashKeySerializer(serializer);
//
//        // 设置值（value）的序列化采用FastJsonRedisSerializer。
//        ProtoStuffRedisSerializer<Object> protoStuffRedisSerializer = new ProtoStuffRedisSerializer<>();
//        redisTemplate.setValueSerializer(protoStuffRedisSerializer);
//        redisTemplate.setHashValueSerializer(protoStuffRedisSerializer);
//        redisTemplate.afterPropertiesSet();
//
//        return redisTemplate;
//    }
//
//
//    @Bean
//    public HlCacheManager myCacheManager(RedisTemplate<String, Object> redisTemplate, CacheMonitorProperties cacheMonitorProperties) {
//        LayeringCacheManager layeringCacheManager = new LayeringCacheManager(redisTemplate,cacheMonitorProperties);
//        // 默认开启统计功能
//        layeringCacheManager.setStats(cacheMonitorProperties.isStats());
//        return layeringCacheManager;
//    }
//
//
//    /**
//     * 此方法名一定要为servletRegistrationBean,否则不会扫描
//     * @param properties
//     * @return
//     */
////    @Bean
////    public ServletRegistrationBean servletRegistrationBean(LayeringCacheProperties properties) {
////        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
////        registrationBean.setServlet(new LayeringCacheServlet());
////        registrationBean.addUrlMappings(!StringUtils.isEmpty(properties.getUrlPattern()) ? properties.getUrlPattern() : "/cache-monitor/*");
////        registrationBean.addInitParameter("loginUsername", StringUtils.isEmpty(properties.getLoginUsername()) ? "admin" : properties.getLoginUsername());
////        registrationBean.addInitParameter("loginPassword", StringUtils.isEmpty(properties.getLoginPassword()) ? "admin" : properties.getLoginPassword());
////        registrationBean.addInitParameter("enableUpdate", properties.isEnableUpdate()+"");
////        if (!StringUtils.isEmpty(properties.getAllow())) {
////            registrationBean.addInitParameter("allow", properties.getAllow());
////        }
////        if (!StringUtils.isEmpty(properties.getDeny())) {
////            registrationBean.addInitParameter("deny", properties.getDeny());
////        }
////        return registrationBean;
////    }
////    @Bean
////    public LayeringAspect layeringAspect() {
////        return new LayeringAspect();
////    }
//}
//
